﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    private float lifeTime;
    [RegisterProperty("Bullet")]
    public float maxLifeTime;

    public BulletData bulletData;

    protected virtual void Awake()
    {
        lifeTime = maxLifeTime;
        bulletData = new BulletData
        {
            Bullet = gameObject,
            InitialPosition = transform.position
        };
        GameMgr.Get<IBulletManager>().InputBulletData(bulletData);
    }

    protected virtual void Update()
    {
        if (lifeTime > 0)
        {
            lifeTime -= Time.deltaTime;
        }
        else
        {
            OnRunOutOfLifeTime();
        }
    }

    protected virtual void OnTriggerEnter2D(Collider2D collider)
    {
        switch (collider.gameObject.tag)
        {
            case "Wall":
                /*
                //击中墙体（地面、墙面）
                Vector2 normal;
                if (collider.gameObject.GetComponent<BoxCollider2D>() != null)
                {
                    BoxCollider2D box = collider.gameObject.GetComponent<BoxCollider2D>();
                    new Vector2(transform.position.x, transform.position.y) + GetComponent<Collider2D>().offset - new Vector2(box.transform.position.x, box.transform.position.y) - box.offset


                }
                else
                {
                    normal = Vector2.zero;
                }
                */
                if (collider.gameObject.GetComponent<TrainBody>() != null)
                {
                    OnHitTrain(collider.gameObject.GetComponent<TrainBody>());
                }
                OnHitGround(Vector2.zero);
                break;
            case "Enemy":
                //击中敌人
                if (collider.gameObject.GetComponent<EnemyControl>() != null)
                {
                    OnHitEnemy(collider.gameObject.GetComponent<EnemyControl>());
                }
                break;
            case "Player":
                //击中玩家
                if (collider.gameObject.GetComponent<PlayerControl>() != null)
                {
                    OnHitPlayer(collider.gameObject.GetComponent<PlayerControl>());
                }
                break;
            default:
                break;
        }
    }
    
    /// <summary>
    /// 子弹生命周期结束时触发
    /// </summary>
    public virtual void OnRunOutOfLifeTime()
    {
        OnDestory();
    }

    /// <summary>
    /// 摧毁该子弹
    /// </summary>
    public virtual void OnDestory()
    {
        GameMgr.Get<IBulletManager>().DestroyBullet(bulletData);
    }

    /// <summary>
    /// 击中玩家时触发
    /// </summary>
    /// <param name="player">被击中的玩家</param>
    public virtual void OnHitPlayer(PlayerControl player)
    {

    }

    /// <summary>
    /// 击中地面或墙体时触发
    /// </summary>
    /// /// <param name="normal">击中墙体的法线。只支持BoxCollider2D，其他碰撞体返回0。(未实装)</param>
    public virtual void OnHitGround(Vector2 normal)
    {
        
    }

    /// <summary>
    /// 击中敌人时触发
    /// </summary>
    /// <param name="enemyControl">被击中的玩家</param>
    public virtual void OnHitEnemy(EnemyControl enemyControl)
    {

    }
    
    public virtual void OnHitTrain(TrainBody train)
    {

    }
}
